﻿namespace CatClient.Message.Spi.Heartbeat.Extend
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;

    public static class ProcessPerformanceExtension
    {
        public static PerformanceCounter CurrentProcessCounter(this IProcessPerformance that, string counterName)
        {
            Process process = Process.GetCurrentProcess();
            int num = 0;
            using (IEnumerator<Process> enumerator = (from p in Process.GetProcesses()
                where p.ProcessName.StartsWith(process.ProcessName)
                orderby p.StartTime
                select p).GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    if (enumerator.Current.Id == process.Id)
                    {
                        string str = (num == 0) ? string.Empty : ("#" + num);
                        return new PerformanceCounter("Process", counterName, process.ProcessName + str);
                    }
                    num++;
                }
            }
            return null;
        }

        //[Serializable, CompilerGenerated]
        //private sealed class <>c
        //{
        //    public static readonly ProcessPerformanceExtension.<>c <>9 = new ProcessPerformanceExtension.<>c();
        //    public static Func<Process, DateTime> <>9__0_1;

        //    internal DateTime <CurrentProcessCounter>b__0_1(Process p) => 
        //        p.StartTime;
        //}
    }
}

